package com.jbh.wanandroidkotlin.activity

import android.animation.ValueAnimator
import android.os.Bundle
import android.text.TextUtils
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.jbh.apilibrary.AppData
import com.jbh.apilibrary.bean.CommonListResult
import com.jbh.apilibrary.bean.IntegralBean
import com.jbh.apilibrary.i.ApiCallBack
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.refreshview.PullRefreshHeader
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import kotlinx.android.synthetic.main.activity_integral_list_actitivity.*
import kotlinx.android.synthetic.main.common_list_layout.*

class IntegralListActivity : BaseActivity() {


    /**
     * 记录上拉还是下拉
     */
    private var isPullRefresh = true
    /**
     * 当前页码
     */
    private var page = 0
    /**
     * 文章的总页数
     *
     */
    private var pageAll = 0;

    private val listContent = mutableListOf<IntegralBean>()

    private var integralAdapter:IntegralListAdapter?=null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_integral_list_actitivity)
        integralTask()
        initData()
    }

    private fun initData() {
        mRecyclerView.apply {
           val l = LinearLayoutManager(this@IntegralListActivity)
            l.orientation = LinearLayoutManager.VERTICAL
            layoutManager = l
            integralAdapter = IntegralListAdapter(listContent)
            adapter = integralAdapter
        }

        mSmartRefreshLayout.apply {
            setEnableLoadMore(true)
            setEnableRefresh(true)
            setRefreshHeader(PullRefreshHeader(this@IntegralListActivity))
            setRefreshFooter(ClassicsFooter(this@IntegralListActivity))
            setOnRefreshListener { Refresh() }
            setOnLoadMoreListener {  LoadMore() }
            autoRefresh()
        }
    }


    private fun Refresh() {
        isPullRefresh = true
        page = 0
        listContent.clear()
        integralListTask()
    }


    private fun LoadMore() {
        isPullRefresh = false
        page ++
        if (pageAll >= page){
            integralListTask();
        }else{
            mSmartRefreshLayout?.finishLoadMore(0, true, true)
        }
    }



    private fun integral(num:Int) {
        ValueAnimator.ofFloat(0f,1f)
            .run {
            duration = 2500
            start()
            addUpdateListener {
                val rodio = it.animatedValue as Float
                var va = rodio * num ;
                tvShowNum.text = ""+Math.round(va)
            }
        }
    }


    private fun integralTask(){
        AppData.instance().integralTask(object :ApiCallBack<IntegralBean>{
            override fun success(data: IntegralBean) {
                println("[integralTask] -> success= ${Gson().toJson(data)}" )
                runOnUiThread { integral(data.coinCount) }
            }

            override fun error(error: String) {
                println("[integralTask] -> error= $error" )
            }
        })
    }


    /**
     * 个人积分列表
     *
     */
    private fun integralListTask(){
        AppData.instance().integralListTask(page,object :ApiCallBack<CommonListResult<IntegralBean>>{
            override fun success(data: CommonListResult<IntegralBean>) {
                println("[integralListTask] -> success= ${Gson().toJson(data)}" )

                runOnUiThread {
                    pageAll = data.pageCount
                    println("[indexArticleTask] - > success = $pageAll")
                    listContent.addAll(data.datas)
                    integralAdapter ?.notifyDataSetChanged()
                    if (isPullRefresh) {
                        mSmartRefreshLayout?.finishRefresh()
                    } else {
                        mSmartRefreshLayout?.finishLoadMore(0, true, false)
                    }
                }
            }

            override fun error(error: String) {
                println("[integralListTask] -> error= $error" )
                runOnUiThread {
                    if (isPullRefresh) {
                        mSmartRefreshLayout?.finishRefresh()
                    } else {
                        mSmartRefreshLayout?.finishLoadMore(0, true, true)
                    }
                }
            }
        })
    }



    class IntegralListAdapter(val list:List<IntegralBean>) :
        BaseQuickAdapter<IntegralBean,BaseViewHolder>(R.layout.item_integral_list_layout,list){
        override fun convert(helper: BaseViewHolder, item: IntegralBean) {
            helper.setText(R.id.tv_num,String.format("+%d",item.coinCount))
            val text= item.desc.replace(",","").replace("：","")
            if (!TextUtils.isEmpty(text)
                &&
                text.contains("签到")){
                helper.setText(R.id.tv_title,text.substring(text.indexOf("签到"),text.length-1))
                helper.setText(R.id.tv_subTitle,text.substring(0,text.indexOf("签到")))
            }
        }
    }
}